package cn.edu.ecnu.gis.client.view;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class FiveOneMap extends HTML {

    private static int nextId = 0;
    private JavaScriptObject fiveonemap;
    private JavaScriptObject control;

    public FiveOneMap() {
        String id = "51map" + (++nextId);
        setStyleName("gwtapps-GoogleMap");
        setHTML("<div id='" + id + "' style='height:100%'></div>");
        RootPanel.get().add(this);

        fiveonemap = initMap(id);
        control = initControl();
    }

    public void setCenter(String cityName, int zoon) {
        setCityNameAndZoom(fiveonemap, cityName, zoon);
    }

    public void addControl() {
        addControl(fiveonemap, control);

    }

    public void addMarker(JSONArray point) {
        addMarker(fiveonemap, point);

    }

    private native static JavaScriptObject initControl()/*-{
    var control= new $wnd.LTStandMapControl();
    return control;
    }-*/;

    private native static JavaScriptObject initMap(String id)/*-{
    var fiveonemap = new $wnd.LTMaps( $doc.getElementById(id) );
    return fiveonemap;
    }-*/;

    private native static void setCityNameAndZoom(JavaScriptObject map, String cityname, int zoon)/*-{
    map.cityNameAndZoom(cityname, zoon);
    }-*/;

    private native static void addControl(JavaScriptObject map, JavaScriptObject control)/*-{
    map.addControl(control)  ;
    }-*/;
//[instance-expr.]@class-name::method-name(param-signature)(arguments)
    private native static void addMarker(JavaScriptObject map, JSONArray point)/*-{
    map.clearOverLays();
    var lat = point.@com.google.gwt.json.client.JSONArray::get(I)(0);
    var lon=point.@com.google.gwt.json.client.JSONArray::get(I)(1);
    var point1=new $wnd.LTPoint(lat,lon);
    var marker=new $wnd.LTMarker(point1);
    map.addOverLay(marker);
    //var LTEvent=new ;
    $wnd.LTEvent.bind(marker,"click",marker,function(){this.openInfoWinHtml("name")});
    map.centerAndZoom(point1,2);
    }-*/;
}
